package com.hl.sfzy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.Consultants;
import com.hl.sfzy.entity.HeartMark;
import com.hl.sfzy.service.HeartMarkService;
import com.hl.sfzy.utils.R;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/hearmark")
public class HeartMarkController {

    @Autowired
    private HeartMarkService heartMarkService;

    @PostMapping("add")
    public R createHeartMark(@RequestBody HeartMark heartMark) {
        int result = heartMarkService.insert(heartMark);
        if (result > 0) {
            return R.ok(heartMark);
        } else {
            return R.fail("插入记录失败");
        }
    }

    // 根据id删除记录
    @DeleteMapping("/{id}")
    public R deleteHeartMarkById(@PathVariable int id) {
        int result = heartMarkService.deleteById(id);
        if (result > 0) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    // 更新记录
    @PutMapping("/update")
    public R updateHeartMark(@RequestBody HeartMark heartMark) {
        int result = heartMarkService.update(heartMark);
        if (result > 0) {
            return R.ok(heartMark);
        } else {
            return R.fail("操作失败");
        }
    }

    // 根据id查询单条记录
    @GetMapping("/{id}")
    public R getHeartMarkById(@PathVariable int id) {
        HeartMark heartMark = heartMarkService.selectById(id);
        if (heartMark != null) {
            return R.ok(heartMark);
        } else {
            return R.fail("查询失败");
        }
    }

    // 查询所有记录
    @GetMapping("/getall")
    public R getAllHeartMarks(@RequestParam(required = false ,defaultValue = "1")int pageNum,
                              @RequestParam(required = false,defaultValue = "5") int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<HeartMark> info = new PageInfo<>(heartMarkService.selectAll());
        return R.ok(info);
    }



}
